﻿using System;
using System.IO;
using System.Windows.Forms;
using RSuite.Services;
using System.Collections.Generic;

namespace RSuite.R1.Services
{
    public class LocalService : LocalServiceBase
    {
        protected override object Process(string method, dynamic parameters, string taskId)
        {
            switch (method)
            {
                case "open":
                    return this.Open(parameters);
                case "save":
                    return this.Save(parameters["filename"], parameters["content"]);
                case "open-article":
                    return this.OpenArticle(parameters);
                case "save-article":
                    return this.SaveArticle(parameters);
                case "get-articles":
                    return this.GetArticles(parameters);
                default:
                    return null;
            }
        }

        private object Open(string filename)
        {
            var dialog = new OpenFileDialog();
            dialog.FileName = filename;
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return null;
            }

            return new
            {
                filename = dialog.FileName,
                content = File.ReadAllText(dialog.FileName)
            };
        }

        private object Save(string filename, string content)
        {
            if (string.IsNullOrEmpty(filename))
            {
                var dialog = new SaveFileDialog();
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return null;
                }
                filename = dialog.FileName;
            }

            File.WriteAllText(filename, content);

            return filename;
        }

        private object OpenArticle(string id)
        {
            var path = Path.Combine(this.GetArticleFolder(), id);
            if (!File.Exists(path))
            {
                return new { status = 404, message = "Article not found" };
            }

            return new
            {
                id = id,
                source = File.ReadAllText(path)
            };
        }

        private object SaveArticle(dynamic data)
        {
            var id = data["id"];
            if (string.IsNullOrEmpty(id))
            {
                id = DateTime.Now.ToString("yyyyMMddHHmmss")+".md";
            }

            File.WriteAllText(Path.Combine(this.GetArticleFolder(), id), data["source"]);

            return id;
        }

        private object GetArticles(dynamic data)
        {
            var dir = new DirectoryInfo(this.GetArticleFolder());
            var articles = new List<object>();

            foreach (var file in dir.GetFiles("*.md"))
            {
                articles.Add(new
                {
                    id = file.Name,
                    source = File.ReadAllText(file.FullName)
                });
            }

            return articles;
        }

        private string GetArticleFolder()
        {
            var path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Articles");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
    }
}